
export default class Poker {
  constructor(pokerName,x, y, width, context){
    this.pokerName = pokerName;
    this.image = wx.createImage();
    this.image.src = './images/' + pokerName + '.jpg';
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = 100;
    this.context = context;
    this.isCalculated = false;
    this.originalX = x;
    this.originalY = y;
    this.gameInfo;
    this.initEvent();
}

  draw(x, y){
  
    this.context.drawImage(this.image, x, y, this.width, this.height);

}

initEvent(){
  canvas.addEventListener('touchstart', ((e) => {
    e.preventDefault();

    let x = e.touches[0].clientX;
    let y = e.touches[0].clientY;
    
//判断触摸点是否在扑克牌的范围内
       if(y > this.y && y < this.y + this.height && x > this.x && x < this.x + this.width){
      
         if (this.isCalculated == false){//未放进空白扑克区内

         this.gameInfo.blankPokers.forEach(
           (blankPoker) => {
             this.x = blankPoker.x;
             this.y = blankPoker.y;
             this.gameInfo.blankPokers.splice(this.gameInfo.blankPokers.indexOf(blankPoker),1);
           }
         );
         } else {//已放进空白扑克区内
           //扑克牌归位
           this.x = this.originalX;
           this.y = this.originalY;
           //重画空白扑克牌
           if (this.gameInfo.blankPokers.length == 1){
             if (this.gameInfo.blankPokers.indexOf(this.gameInfo.blankPoker1) >= 0){
               this.gameInfo.blankPokers.push(this.gameInfo.blankPoker2);
             } else if (this.gameInfo.blankPokers.indexOf(this.gameInfo.blankPoker2) >= 0){
               this.gameInfo.blankPokers = [this.gameInfo.blankPoker1, this.gameInfo.blankPoker2];
             }
           } else if (this.gameInfo.blankPokers.length == 0){
             if (y > this.gameInfo.blankPoker1.y && y < this.gameInfo.blankPoker1.y + this.gameInfo.blankPoker1.height && x > this.gameInfo.blankPoker1.x && x < this.gameInfo.blankPoker1.x + this.gameInfo.blankPoker1.width){//点击第一张空白扑克牌区域
               this.gameInfo.blankPokers = [this.gameInfo.blankPoker1];
             } else if (y > this.gameInfo.blankPoker2.y && y < this.gameInfo.blankPoker2.y + this.gameInfo.blankPoker2.height && x > this.gameInfo.blankPoker2.x && x < this.gameInfo.blankPoker2.x + this.gameInfo.blankPoker2.width) {//点击第一张空白扑克牌区域
               this.gameInfo.blankPokers = [this.gameInfo.blankPoker2];
            }
           }
           }
              this.isCalculated = !this.isCalculated;
         this.gameInfo.render();    
    }
  }).bind(this));
}

getPoint() {
  console.log(this.image.src);
  var point = this.pokerName.replace(/[^0-9]/ig, "");
  if (point > 10) {
    point = 10;
  }
  return point;
}
}